// pages/myMessage/myMessage.js
var app = getApp();
const http = require('../../utils/http.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navId: 0,
    messageList:[],
    leavingList:[]
  },
  //nav栏点击
  selectNav(e) {
    var id = e.currentTarget.dataset.id;
    this.setData({
      navId: id
    })

    if (id == 0) {
      this.getMessage(1)
    } else if (id == 1) {
      this.getLeaving(1);
    }

  },
//跳转帖子留言列表
  toMessage(e){
    var item=e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pages/dynamicDetail/dynamicDetail?pid=' + item.post_id +'&type=1',
    })
  },
  moreOpt(e){
    var mid=e.currentTarget.dataset.mid;
    wx.showActionSheet({
      itemList: ['删除'],
      itemColor:'#FF3333',
      success:(res)=> {
        console.log(res.tapIndex)
        if (res.tapIndex==0){
          //删除消息
          http.fetchApi('procedure_t', { p: `R2020009|${mid}` }).then(res1=>{
            if(res1.data.code==1){
              wx.showToast({ title: '删除成功'});
              setTimeout(()=>{
                this.getMessage(1)
              },1000)
            }
          })
        }
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },

  moreOpt1(e){
    var lid=e.currentTarget.dataset.lid;
    var is_zd = e.currentTarget.dataset.zd;
    var itemList;
    if (is_zd==0){
      itemList = ['置顶', '删除']
    }else{
      itemList = ['不再置顶', '删除']
    }
    wx.showActionSheet({
      itemList,
      itemColor: '#FF3333',
      success:(res)=> {
        console.log(res.tapIndex)
        if (res.tapIndex==0){
          //置顶
          http.fetchApi('procedure_t', { p: `R2020010|${lid}` }).then(res1 => {
            if (res1.data.code == 1) {
              if (itemList[0]=='置顶'){
                wx.showToast({ title: '置顶成功' });
              }else{
                wx.showToast({ title: '取消置顶' });
              }
              
              setTimeout(() => {
                this.getLeaving(1)
              }, 1000)
            }
          })
        }else{
          //删除留言
          http.fetchApi('procedure_t', { p: `R2020009|${lid}` }).then(res1 => {
            if (res1.data.code == 1) {
              wx.showToast({ title: '删除成功' });
              setTimeout(() => {
                this.getLeaving(1)
              }, 1000)
            }
          })
        }
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },

  //获取我的消息
  getMessage(page){
    wx.showLoading({
      title: '加载中',
    })
    var uid = app.globalData.uid;
    http.fetchApi('procedure_t', { p: `R2019001|${uid}|1|${page}|10` }).then(res=>{
      wx.hideLoading();
      var messageList = this.data.messageList;
      var messageList1 = res.data.results;
     
      if (page == 1) {
        messageList = res.data.results;
      } else {
        messageList = [...messageList, ...messageList1]
      }
      if (messageList1.length == 0 && page > 1) {
        http.showToast('暂无更多!' + '', '', 'none')
        return
      }
    
     
      this.setData({
        messageList,
        page
      })
    })
  },

  //获取我的留言
  getLeaving(page1){
    wx.showLoading({
      title: '加载中',
    })
    var uid = app.globalData.uid;
    http.fetchApi('procedure_t', { p: `R2019001|${uid}|2|${page1}|10` }).then(res => {
      wx.hideLoading();
      var leavingList = this.data.leavingList;
      var leavingList1 = res.data.results;
     
      if (page1 == 1) {
        leavingList = res.data.results;
      } else {
        leavingList = [...leavingList, ...leavingList1]
      }
      if (leavingList1.length == 0 && page1 > 1) {
        http.showToast('暂无更多!' + '', '', 'none')
        return
      }
      this.setData({
        leavingList,
        page1
      })
    })
  },

  // //判断是否有消息未查看
  // isMesCheck(){
   
  // },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMessage(1)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.navId == 0) {
      var page = this.data.page || 1;
      page++;
      this.getMessage(page);
    } else if (this.data.navId == 1) {
      var page1 = this.data.page1 || 1;
      page1++;
      this.getLeaving(page1);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})